/*
	       File: FileDelete.cs
	Create Date: 2007-08-13
	    Version:

	Description:


	 Change log:


*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor;
using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor.Activities
{
	public partial class FileDelete: Activity
    {
        #region Private member fields
        private string _Filename;
        #endregion

        #region Constructors and cleanup methods
        public FileDelete()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        #endregion

        #region Class properties
        public static DependencyProperty FilenameProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Filename", typeof(string), typeof(FileDelete));
        [Description("Path to the file to be deleted.")]
        [Category("File Delete")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Filename
        {
            get { return _Filename; }
            set { _Filename = value; }
        }
        #endregion

        #region Events handlers
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Logger.LogEnter();
            IProcessorDataService dataService = executionContext.GetService(typeof(IProcessorDataService)) as IProcessorDataService;
            if (null == dataService)
            {
                throw new InvalidOperationException("Can't get the service!");
            }

            try
            {
                WorkItem workItem = dataService.GetWorkItem();
                StringProcessor stringProcessor = new StringProcessor(new FileProcessorSymbolTable(dataService.GetConfiguration(), workItem));
                string processedFilename = stringProcessor.SubstituteStringValues(_Filename);

                Logger.LogInfo("FileDelete", "Deleting file: {0}", processedFilename);
                if (true == File.Exists(processedFilename))
                {
                    File.Delete(processedFilename);
                }
                else
                {
                    Logger.LogWarning("FileDelete", "The file does not exist!");
                }
            }
            catch (Exception exc)
            {
                Logger.LogError("FileDelete", "An error occured while attempting to copy a file.\nError: {0}", exc.Message);
                throw exc;
            }

            Logger.LogLeave();
            return base.Execute(executionContext);
        }
        #endregion

        #region Public interface
        #endregion
	}
}
